<?php
	session_start();
	if(isset($_SESSION["giohang"]) && count($_SESSION["giohang"]) > 0)
	{
		include("../code/DataProvider.php");
		
		$date = date("y-m-d g:i:s");
		//Tao moi don dat hang
		$sql = "INSERT INTO `dondathang`(`manguoidung`, `ngay`, `matrangthai`) VALUES (".$_SESSION["manguoidung"].",'".$date."',1)";
		
		DataProvider::ExecuteQuery($sql);
		
		$sql = "SELECT ma FROM dondathang ORDER BY ma DESC LIMIT 0,1";
		$result = DataProvider::ExecuteQuery($sql);
		$row = mysql_fetch_array($result);
		$maDonDatHang = $row["ma"];
		
		for($i = 0; $i < count($_SESSION["giohang"]); $i++)
		{
			$id = $_SESSION["giohang"][$i];
			$sql = "SELECT * FROM dt WHERE madt = ".$id;
			
			$result = DataProvider::ExecuteQuery($sql);
			$row = mysql_fetch_array($result);
			$gia = (int)$row["gia"];
			$sql = "INSERT INTO `chitietdondathang`(`madondathang`, `madt`, `soluong`, `giaban`) VALUES (".$maDonDatHang.",".$id.",".$_SESSION["soluong"][$i].",".$gia.")";
			
			DataProvider::ExecuteQuery($sql);
		}
		
		unset($_SESSION["giohang"]);
		unset($_SESSION["soluong"]);
	}
	header("location:../index.php");
?>